package com.info33.platform.doc.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.info33.platform.common.mvc.vo.Result;
import com.info33.platform.doc.dao.DocFileDao;
import com.info33.platform.doc.dao.DocFileShareDao;
import com.info33.platform.doc.dao.DocFileUserDao;
import com.info33.platform.doc.dto.DocFileSearchDTO;
import com.info33.platform.doc.dto.DocFileShareDTO;
import com.info33.platform.doc.dto.DocFileUserSaveDTO;
import com.info33.platform.doc.entity.DocFile;
import com.info33.platform.doc.entity.DocFileShare;
import com.info33.platform.doc.entity.DocFileUser;
import com.info33.platform.doc.service.DocFileShareService;
import com.info33.platform.doc.service.DocFileUserService;
import com.info33.platform.doc.vo.DocFileVO;
import com.info33.platform.system.vo.SysUserVO;

/**
 * @author admin
 */
@Service
@Transactional(rollbackFor = {Exception.class, Error.class})
public class DocFileShareServiceImpl extends ServiceImpl<DocFileShareDao, DocFileShare> implements DocFileShareService {

    @Autowired
    private DocFileShareDao docFileShareDao;
    @Autowired
    private DocFileUserService docFileUserService;
    @Autowired
    private DocFileUserDao docFileUserDao;
    @Autowired
    private DocFileDao docFileDao;

	@Override
	public Result<?> shareFile(DocFileShareDTO docFileShareDTO) {
		DocFile docFile = docFileDao.selectById(docFileShareDTO.getFileId());
		if(ObjectUtils.isEmpty(docFile)) {
			return Result.error("此文档不存在");
		}
		DocFileShare docFileShare = docFileShareDao.selectOne(new QueryWrapper<DocFileShare>().eq("file_id", docFileShareDTO.getFileId()));
		List<DocFileUser> docFileUserList = docFileShareDTO.getDocFileUserList();
		//权限为空，取消分享
		if(CollectionUtils.isEmpty(docFileUserList)) {
			if(!ObjectUtils.isEmpty(docFileShare)) {
				docFileShareDao.delete(new QueryWrapper<DocFileShare>().eq("file_id", docFileShareDTO.getFileId()));
			}
		}
		
		if(ObjectUtils.isEmpty(docFileShare)) {
			docFileShare = new DocFileShare();
			docFileShare.setFileId(docFileShareDTO.getFileId());
			if(docFileShareDao.insert(docFileShare) == 0) {
				return Result.error();
			}
		}
		
		DocFileUserSaveDTO docFileUserSaveDTO = new DocFileUserSaveDTO();
		docFileUserSaveDTO.setFileId(docFileShareDTO.getFileId());
		docFileUserSaveDTO.setDocFileUserList(docFileUserList);
		return docFileUserService.saveFileUser(docFileUserSaveDTO);
	}

	@Override
	public Result<?> calcelShare(Long fileId) {
		DocFile docFile = docFileDao.selectById(fileId);
		if(ObjectUtils.isEmpty(docFile)) {
			return Result.error("此文档不存在");
		}
		DocFileShare docFileShare = docFileShareDao.selectOne(new QueryWrapper<DocFileShare>().eq("file_id", fileId)); 
		if(ObjectUtils.isEmpty(docFileShare)) {
			return Result.error("此文档没有共享");
		}
		if(docFileUserDao.delete(new QueryWrapper<DocFileUser>().eq("file_id", fileId).ne("user_id", docFile.getCreateBy())) == 0) {
			return Result.error();
		}
		return docFileShareDao.delete(new QueryWrapper<DocFileShare>().eq("file_id", fileId)) > 0 ? Result.ok() : Result.error();
	}

	@Override
	public IPage<DocFileVO> pageSharedFileList(IPage<DocFileVO> page, DocFileSearchDTO docFileSearchDTO,
			SysUserVO currentUser) {
		return docFileShareDao.pageSharedFileList(page, docFileSearchDTO, currentUser);
	}

	@Override
	public IPage<DocFileVO> pageShareWithMeFile(IPage<DocFileVO> page, DocFileSearchDTO docFileSearchDTO,
			SysUserVO currentUser) {
		return docFileShareDao.pageShareWithMeFile(page, docFileSearchDTO, currentUser);
	}

}